package com.atguigu.auth.controller;

import com.atguigu.auth.service.SysMenuService;
import com.atguigu.auth.service.SysRoleMenuService;
import com.atguigu.auth.service.SysUserService;
import com.atguigu.common.config.exception.GuiguException;
import com.atguigu.common.jwt.JwtHelper;
import com.atguigu.common.result.Result;
import com.atguigu.model.system.SysMenu;
import com.atguigu.model.system.SysRole;
import com.atguigu.model.system.SysRoleMenu;
import com.atguigu.model.system.SysUser;
import com.atguigu.vo.system.LoginVo;
import com.atguigu.vo.system.RouterVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import lombok.experimental.Helper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Api(tags = "后台登录管理")
@RestController
@RequestMapping("/admin/system/index")
@Slf4j
public class IndexController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    //login
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo) {
//        {"code":200,"data":{"token":admin-token"}}
//        Map<String,Object> map = new HashMap<>();
//        map.put("token","admin-token");
//        return Result.ok(map);
        //判断用户名，密码，
        SysUser sysUser = sysUserService.selectUserByUserNameAndPwd(loginVo);
        String token = JwtHelper.createToken(sysUser.getId(), sysUser.getUsername());
        Map<String,Object> map = new HashMap<>();
        map.put("token",token);
        return Result.ok(map);
    }

    //info
    @GetMapping("info")
    public Result info(HttpServletRequest request) {
        //1.从请求头获取用户信息（获取请求头token字符串）
        String token = request.getHeader("token");
        //2.从token字符串获取用户ID或者用户名称
        Long userId = JwtHelper.getUserId(token);
        String username = JwtHelper.getUsername(token);
        log.info("userId====={},username========{}",userId,username);
        //3.根据用户ID查询数据库，把用户信息获取出来。
        SysUser sysUser = sysUserService.getById(userId);
        if(ObjectUtils.isEmpty(sysUser)){
            throw new GuiguException(500,"用户不存在");
        }
        String headUrl = sysUser.getHeadUrl();
        //获取菜单权限
        List<RouterVo> menuList = sysMenuService.findMenuByUserId(userId);
        //获取按钮权限
        List<String> permsList = sysMenuService.findPermsByUserId(userId);
//
//        List<String> routers = new ArrayList<>();
//        List<String> buttons = new ArrayList<>();
//        //4.根据用户id获取用户可以操作的菜单
//        for (SysRole sysRole : roleList) {
//            Long roleId = sysRole.getId();
//            LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(SysRoleMenu::getRoleId,roleId);
//            List<SysRoleMenu> list = sysRoleMenuService.list(wrapper);
//            for (SysRoleMenu sysRoleMenu : list) {
//                SysMenu sysMenu = sysMenuService.getById(sysRoleMenu.getMenuId());
//                if(sysMenu.getType()==1){
//                    routers.add(sysMenu.getName());
//                }
//                if(sysMenu.getType() == 2){
//                    buttons.add(sysMenu.getName());
//                }
//            }
//        }
        //5.根据用户id获取用户可以操作的按钮
        Map<String, Object> map = new HashMap<>();
        map.put("roles","[admin]");
        map.put("name",sysUser.getName());
        map.put("avatar",headUrl);
        //TODO 返回用户可以操作菜单，项目
        map.put("routers",menuList);
        //TODO 返回用户可以操作按钮,菜单
        map.put("buttons",permsList);
        return Result.ok(map);
    }

    @PostMapping("logout")
    public Result logout(){
        return Result.ok();
    }
}
